package maxent.mr.util;



import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author hadoop
 */
public class CountLines {

    public static int getLines(String path) throws IOException{
        
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(path), conf);
        FileStatus[] files = fs.listStatus(new Path(path));
        String fileName = files[0].getPath().getName();
        path = path+"/"+ fileName;
        
        FSDataInputStream in = fs.open(new Path(path));
        
        try {
            byte[] c = new byte[1024];
            int count = 1;
            int readChars = 0;
            while ((readChars = in.read(c)) != -1) {
                for (int i = 0; i < readChars; ++i) {
                    if (c[i] == '\n')
                        ++count;
                }
            }
            return count;
        } finally {
            in.close();
        }


    }

}
